/************************************************************************************/
/*  Program name CAPFAIMP_DIST_ACCTS.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 17 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_DIST_ACCTS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'loge.log';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;
	lv_counter		VARCHAR(100);
	
	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_DIST_ACCTS;
	
	lv_COA_ID		NUMBER;

BEGIN
	lv_counter := 0;
	
	UPDATE
		FA_DISTRIBUTION_ACCOUNTS
	SET	
		BOOK_TYPE_CODE = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_DIST_ACCTS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			
		lv_counter := lv_counter + 1;
     
     	lv_UTL_File := UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
     	UTL_FILE.PUT_LINE(lv_UTL_File, lv_counter);
	 	UTL_FILE.FClose(lv_UTL_File);
	 
			lv_COA_ID := CAPBRIMP_COMMON.GetCOAIDfromBTC(C1.Book_Type_Code);
			
			INSERT INTO FA_DISTRIBUTION_ACCOUNTS
			VALUES
			(
			C1.BOOK_TYPE_CODE,
			CAPBRIMP_MAPPING.GetFADistIDMapping(C1.DISTRIBUTION_ID),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ASSET_COST_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ASSET_CLEARING_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DEPRN_EXPENSE_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DEPRN_RESERVE_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CIP_COST_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CIP_CLEARING_ACCOUNT_CCID_D),
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.NBV_RETIRED_GAIN_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.NBV_RETIRED_LOSS_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PROCEEDS_SALE_GAIN_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PROCEEDS_SALE_LOSS_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.COST_REMOVAL_GAIN_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.COST_REMOVAL_LOSS_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PROCEEDS_SALE_CLEARING_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.COST_REMOVAL_CLEARING_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.REVAL_RSV_GAIN_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.REVAL_RSV_LOSS_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DEFERRED_EXP_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DEFERRED_RSV_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.DEPRN_ADJ_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.REVAL_AMORT_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.REVAL_RSV_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BONUS_EXP_ACCOUNT_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BONUS_RSV_ACCOUNT_CCID_D)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/